package gemini.castor.ui.client.page.content.home.register;

import gemini.castor.ui.client.mvp.container.CastorPlaceContainer;
import gemini.castor.ui.client.mvp.container.CastorPlaceContainer.PresenterGetter;
import gemini.castor.ui.client.mvp.container.CastorWidgetContainerDisplay;
import gemini.castor.ui.client.mvp.container.CastorWidgetContainerPresenter;
import gemini.castor.ui.client.mvp.gin.CastorGinjector;
import gemini.castor.ui.client.utils.PlaceConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;
import ch.elca.gwt.mvp.client.place.PlaceRequest;

import com.google.inject.Inject;

public class RegisterPresenter extends
		CastorWidgetContainerPresenter<RegisterPresenter.Display> {
	
	
	public interface Display extends CastorWidgetContainerDisplay {
	}

	public static final Place PLACE = new CastorPlaceContainer<RegisterPresenter>(
			PlaceConstants.PLACE_REGISTER, new PresenterGetter<RegisterPresenter>() {
				public RegisterPresenter getPresenter() {
					return CastorGinjector.INSTANCE.getRegisterPresenter();
				}
			});

	@Inject
	public RegisterPresenter(Display display, EventBus eventBus) {
		super(display, eventBus);
		addPresenter(CastorGinjector.INSTANCE.getDistributorIDPresenter());
		addPresenter(CastorGinjector.INSTANCE.getInputAccountFormPresenter());
		addPresenter(CastorGinjector.INSTANCE.getConfirmInfoFormPresenter());
	}

	/*
	 * Register listeners on Display actions
	 */
	@Override
	protected void onBind() {
		super.onBind();
		
		registerHandlerRegister();
		
		showDefaultPresenter();
	}

	private void registerHandlerRegister() {
		registerHandler(getLocalEventBus().addHandler(RegisterEvent.getType(), new RegisterHandler() {



			@Override
					public void onRegister(RegisterEvent homeEvent) {
						showPresenter(CastorGinjector.INSTANCE
								.getConfirmInfoFormPresenter(),
								getCurrentOutObject());
					}

			@Override
			public void onSubmit(RegisterEvent registerEvent) {
						showPresenter(CastorGinjector.INSTANCE
								.getInputAccountFormPresenter(),
								getCurrentOutObject());
			}

			@Override
			public void onCancel(RegisterEvent registerEvent) {
				showDefaultPresenter();
			}

			@Override
			public void onBack(RegisterEvent registerEvent) {
				showPresenter(CastorGinjector.INSTANCE.getInputAccountFormPresenter());
			}

			@Override
			public void onConfirm(RegisterEvent registerEvent) {
				showDefaultPresenter();
			}

		}));
	}
	
	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	protected void onPlaceRequest(PlaceRequest request) {
		// TODO Auto-generated method stub
	}
	
	private Object getCurrentOutObject(){
		return getCurrentPresenter().getOutputObject();
	}
}
